package com.mofofa.maozy.components.form

import android.annotation.SuppressLint
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.data.RevObjInfoPageFieldConfig
import com.mofofa.maozy.data.DataWalletChannelFcfa
import com.mofofa.maozy.databinding.ViewMaozyFormSelectBindWalletFcfaBinding
import com.mofofa.maozy.extend.*

/**
<declare-styleable name="MaozyFormSelect">
<attr name="cwfsbwfcfaTitle" format="string" />
<attr name="cwfsbwfcfaText" format="string" />
<attr name="cwfsbwfcfaHint" format="string" />
<attr name="cwfsbwfcfaTitleColor" format="color" />
<attr name="cwfsbwfcfaTextColor" format="color" />
<attr name="cwfsbwfcfaHintColor" format="color" />
<attr name="cwfsbwfcfaTitleSize" format="dimension" />
<attr name="cwfsbwfcfaTextSize" format="dimension" />
<attr name="cwfsbwfcfaIsTitleBold" format="boolean" />
</declare-styleable>
 * */
class FcfaSelectWalletForm : RelativeLayout,
    com.mofofa.maozy.components.form.BaseFormAction,
    BaseFormAction {

    private lateinit var vMaozyRootMaozyView: View
    lateinit var mMaozyViewMaozyBinding: ViewMaozyFormSelectBindWalletFcfaBinding
    lateinit var mMaozyPopMaozyListMaozyAdapter: FcfaSelectWalletAdapter

    private var cwfsbwfcfaTitleColor: Int = 0
    private var cwfsbwfcfaTextColor: Int = 0
    private var cwfsbwfcfaHintColor: Int = 0
    private var cwfsbwfcfaTitle: String? = null
    private var cwfsbwfcfaText: String? = null
    private var cwfsbwfcfaHint: String? = null
    private var cwfsbwfcfaTitleSize: Float = 0F
    private var cwfsbwfcfaTextSize: Float = 0F
    private var cwfsbwfcfaIsTitleBold: Boolean = false

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null
    private var onMaozyFormMaozyConfig: RevObjInfoPageFieldConfig? = null
    private var OnMaozyStateMaozyChangedMaozyListener: (Int, String) -> Unit =
        { action, content -> }

    private var mMaozyContentMaozyValue: String = ""
    private var mMaozyTempMaozyInputMaozyValue: String = ""
    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int) -> Unit = {}
    private var mMaozyOnMaozyTextMaozyChangedMaozyListener: (String) -> Unit =
        { text -> }
    private var mMaozyOnMaozyListMaozyItemMaozyClickMaozyListener: (String, String) -> Unit =
        { display, content -> }
    private val mMaozyDataMaozyList = mutableListOf<DataWalletChannelFcfa>()

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array =
                    cxt.obtainStyledAttributes(attrs, R.styleable.FcfaSelectWalletForm)
                cwfsbwfcfaTitleColor = array.getColor(
                    R.styleable.FcfaSelectWalletForm_cwfsbwfcfaTitleColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwfsbwfcfaTitle =
                    array.getString(R.styleable.FcfaSelectWalletForm_cwfsbwfcfaTitle)

                cwfsbwfcfaTextColor = array.getColor(
                    R.styleable.FcfaSelectWalletForm_cwfsbwfcfaTextColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwfsbwfcfaText =
                    array.getString(R.styleable.FcfaSelectWalletForm_cwfsbwfcfaText)

                cwfsbwfcfaHintColor = array.getColor(
                    R.styleable.FcfaSelectWalletForm_cwfsbwfcfaHintColor,
                    ContextCompat.getColor(cxt, R.color.input_hint)
                )
                cwfsbwfcfaHint =
                    array.getString(R.styleable.FcfaSelectWalletForm_cwfsbwfcfaHint)

                cwfsbwfcfaTitleSize = array.getDimension(
                    R.styleable.FcfaSelectWalletForm_cwfsbwfcfaTitleSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwfsbwfcfaTextSize = array.getDimension(
                    R.styleable.FcfaSelectWalletForm_cwfsbwfcfaTextSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwfsbwfcfaIsTitleBold = array.getBoolean(
                    R.styleable.FcfaSelectWalletForm_cwfsbwfcfaIsTitleBold,
                    false
                )

                array.recycle()
            }
        }
        context?.let {
            vMaozyRootMaozyView =
                View.inflate(it, R.layout.view_maozy_form_select_bind_wallet_fcfa, this)
            mMaozyViewMaozyBinding = ViewMaozyFormSelectBindWalletFcfaBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.text = (cwfsbwfcfaTitle)
            mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.setTextColor(cwfsbwfcfaTitleColor)
            mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwfsbwfcfaTitleSize
            )

            mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.setText(cwfsbwfcfaText)
            mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.setTextColor(cwfsbwfcfaTextColor)
            mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwfsbwfcfaTextSize
            )

            mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.hint = (cwfsbwfcfaHint)
            mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.setHintTextColor(cwfsbwfcfaHintColor)

            mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.addTextChangedListener(object :
                TextWatcher {
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun afterTextChanged(p0: Editable?) {
                    //判断是否显示删除按键
                    val tempContent = getMaozyContent()
                    //判断是否值已经变化
                    try {
                        if (tempContent.isNotBlank()) {
                            if (!mMaozyTempMaozyInputMaozyValue.equals(tempContent, false)) {
                                mMaozyTempMaozyInputMaozyValue = tempContent
                                recordMaozySelectMaozyEvent(tempContent)
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            })

            mMaozyViewMaozyBinding.vFormSelectBindWalletFCFAAction.setOnClickListener {
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
                if (mMaozyDataMaozyList.isNotEmpty()) {
                    if (mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFADataListBg.visibility == VISIBLE) {
                        mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFADataListBg.visibility = GONE
                    } else {
                        mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFADataListBg.visibility =
                            VISIBLE
                        mMaozyPopMaozyListMaozyAdapter.notifyDataSetChanged()
                    }
                } else {
                    mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFADataListBg.visibility = GONE
                }
            }

            mMaozyPopMaozyListMaozyAdapter =
                FcfaSelectWalletAdapter(mMaozyDataMaozyList)
            mMaozyPopMaozyListMaozyAdapter.setMaozyOnMaozyItemMaozyClickMaozyListener { i, cashVistaPopListItem ->
                setMaozyContentMaozyValue(cashVistaPopListItem.value)
                setMaozyContent("${cashVistaPopListItem.desc}#${cashVistaPopListItem.value}")
                mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFADataListBg.visibility = GONE
                mMaozyOnMaozyListMaozyItemMaozyClickMaozyListener.invoke(
                    cashVistaPopListItem.desc,
                    cashVistaPopListItem.value
                )
            }

            mMaozyViewMaozyBinding.rcFormSelectBindWalletFCFADataList.layoutManager =
                LinearLayoutManager(context)
            mMaozyViewMaozyBinding.rcFormSelectBindWalletFCFADataList.adapter =
                mMaozyPopMaozyListMaozyAdapter

            //增加星号显示
            mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.addMaozyHighlightMaozyStyle(
                mutableListOf("*"),
                ContextCompat.getColor(context, R.color.main_red)
            )

            if (cwfsbwfcfaIsTitleBold) {
                mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.addMaozyBoldMaozyStyle()
            } else {
                mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.addMaozyNormalMaozyStyle()
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setMaozyDetail(detail: String) {
        setMaozyContent(detail)
    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {

    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {

    }

    override fun setMaozyContent(content: String) {
        mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.setText(content)
        val splitStart = content.indexOf("#")
        if (splitStart >= 0) {
            val bankCodeAndSplitChar = content.substring(splitStart)
            mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.addMaozyTipsMaozyStyle(
                words = bankCodeAndSplitChar,
                sizeSpan = 1F,
                ContextCompat.getColor(context, R.color.transparent)
            )
        }
        if (content.isNotBlank()) {
            mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFAContentBg.setBackgroundResource(R.drawable.maozy_form_input)
            mMaozyViewMaozyBinding.textFormSelectBindWalletFCFAErrTip.visibility = GONE
        }
        onMaozyFormMaozyConfig?.fillContent = content
        recordMaozySelectMaozyEvent(content.uppercase())
    }

    fun setMaozyContentMaozyValue(contentValue: String) {
        mMaozyContentMaozyValue = contentValue
    }

    fun getMaozyContent(): String {
        return mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.text.toString().trim()
    }

    fun getMaozyContentMaozyValue(): String {
        return mMaozyContentMaozyValue
    }

    fun getMaozyTitle(): String {
        return mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.text.toString().trim()
    }

    override fun setMaozyTitle(title: String) {
        cwfsbwfcfaTitle = title
        mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.text = (cwfsbwfcfaTitle)
        //增加星号显示
        mMaozyViewMaozyBinding.textFormSelectBindWalletFCFATitle.addMaozyHighlightMaozyStyle(
            mutableListOf("*"),
            ContextCompat.getColor(context, R.color.main_red)
        )
    }

    override fun setMaozyHint(hint: String) {
        cwfsbwfcfaHint = hint
        mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.hint = (cwfsbwfcfaHint)
    }

    override fun isMaozyFormMaozyCompleted(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFAContentBg.setBackgroundResource(R.drawable.maozy_form_input_error)
            mMaozyViewMaozyBinding.textFormSelectBindWalletFCFAErrTip.visibility = VISIBLE
            return false
        }

        mMaozyViewMaozyBinding.textFormSelectBindWalletFCFAErrTip.visibility = GONE
        mMaozyViewMaozyBinding.rlFormSelectBindWalletFCFAContentBg.setBackgroundResource(R.drawable.maozy_form_input)
        return true
    }

    override fun isMaozyFormMaozyCompletedMaozySilent(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            return false
        }
        return true
    }

    override fun setMaozyFormMaozyConfig(item: RevObjInfoPageFieldConfig) {

    }

    override fun setMaozyOnMaozyStateMaozyChangedMaozyListener(listener: (Int, String) -> Unit) {
        OnMaozyStateMaozyChangedMaozyListener = listener
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }

    fun setMaozyOnMaozyTextMaozyChangedMaozyListener(listener: (String) -> Unit) {
        mMaozyOnMaozyTextMaozyChangedMaozyListener = listener
    }

    fun setMaozyOnMaozyListMaozyItemMaozyClickMaozyListener(listener: (String, String) -> Unit) {
        mMaozyOnMaozyListMaozyItemMaozyClickMaozyListener = listener
    }

    fun setMaozyDataMaozyList(dataList: List<DataWalletChannelFcfa>?) {
        dataList?.let { list ->
            mMaozyDataMaozyList.clear()
            mMaozyDataMaozyList.addAll(dataList)
        }
    }

    fun cleanMaozy() {
        mMaozyViewMaozyBinding.editFormSelectBindWalletFCFA.setText(null)
    }
}